<?php
require_once dirname(__FILE__) . '/../config/config.php';
class AppController
{
    private $smarty;
    public function __construct()
    {
    }

    public function main(&$smarty)
    {
        $this->smarty = $smarty;
    }

    public function listAction()
    {
        $context = new BaseContext();
        $result = new ServiceResult();
        $appService = new AppService();
        $pager = new Pager();
        
        $curpage = isset ($_GET['page']) ? $_GET['page'] : 1;
        if(isset($_GET['type']) && !empty($_GET['type']))
        {
        	$type = $_GET['type'];
        	if($type == 'ungdung')
        	{
        		$appNameArr = $appService -> getAppNameByStatus($context, $result, 1);
        		if(isset($_POST) && !empty($_POST))
		        {
	        		$filter = $_POST['filter'];
	        		if(isset($_GET) && !empty($_GET) && $_GET['filter'] == 'filter')
			        {
			        	 $appArr = $appService -> getAllAppByName($context, $result, $filter);
			        }
			        $this->smarty->assign('filter', $filter);
		        }
        		else {
		        	$count = $appService->countAppByStatus($context, $result, 1);
			        $limit = 20;
			        $start = $pager->findStart($limit);
			        $pages = $pager->findPages($count, $limit);
			        $appArr = $appService -> getAppByStatus($context, $result, 1, $start, $limit);
			        $lastStr = "controller=app&action=list&type=ungdung";
			    }
		        $this->smarty->assign('appNameArr', $appNameArr);
        	}
        	else if($type == 'tuvi')
        	{
        		$appNameArr = $appService -> getAppNameByStatus($context, $result, 2);
        		
        		if(isset($_POST) && !empty($_POST))
		        {
	        		$filter = $_POST['filter'];
	        		if(isset($_GET) && !empty($_GET) && $_GET['filter'] == 'filter')
			        {
			        	 $appArr = $appService -> getAllAppByName($context, $result, $filter);
			        }
			        $this->smarty->assign('filter', $filter);
		        }
        		else {
        			
	        		$count = $appService->countAppByStatus($context, $result, 2);
			        $limit = 20;
			        $start = $pager->findStart($limit);
			        $pages = $pager->findPages($count, $limit);
			        $appArr = $appService -> getAppByStatus($context, $result, 2, $start, $limit);
			        $lastStr = "controller=app&action=list&type=tuvi";
        		}
        		
		        $this->smarty->assign('appNameArr', $appNameArr);
        	}
        	$this->smarty->assign('type', $type);
        }
        else 
        {
        	$appNameArr = $appService -> getAppNameByStatus($context, $result, 1);
        	$count = $appService->countApp($context, $result);
	        $limit = 20;
	        $start = $pager->findStart($limit);
	        $pages = $pager->findPages($count, $limit);
	        $appArr = $appService -> getAllApp($context, $result, $start, $limit);
	        $lastStr = "controller=app&action=list";
	        $this->smarty->assign('appNameArr', $appNameArr);
        }
        
        if(isset($pager) && isset($pages) && isset($lastStr))
	    {
	        $pageFirst = $pager->findFirst($curpage, $pages, $lastStr);
	        $pagePrev = $pager->findPrev($curpage, $pages, $lastStr);
	        $pageList = $pager->pageList($curpage, $pages, $lastStr);
	        $pageNext = $pager->findNext($curpage, $pages, $lastStr);
	        $pageLast = $pager->findLast($curpage, $pages, $lastStr);
			
	        $this->smarty->assign('pageFirst', $pageFirst);
	        $this->smarty->assign('pagePrev', $pagePrev);
	        $this->smarty->assign('pageList', $pageList);
	        $this->smarty->assign('pageNext', $pageNext);
	        $this->smarty->assign('pageLast', $pageLast);
	    }
	    if(isset($appArr))
        {
        	$this->smarty->assign('appArr', $appArr);
        }
        $this->smarty->display('list-app.tpl');
    }

    public function addAction()
    {
        $_SESSION['timeout'] = time();
        $context = new BaseContext();
        $result = new ServiceResult();
		$appService = new AppService();
		
    	if(isset($_POST) && !empty($_POST))
        {
            if ($_POST["type"] == 1)
            {
                $folder = 'ungdung';
            }
            else
            {
                $folder = 'tuvi';
            }

            $_POST['smallImage'] = $folder .'/'.'thumbnail'.'/'.$_POST['image'];
            $_POST['mediumImage'] = $folder .'/'.'small'.'/'.$_POST['image'];
            $_POST['largeImage'] = $folder .'/'.'large'.'/'.$_POST['image'];
			
           	$_POST['status'] = $_POST["type"];
           	$_POST['shortDetail'] = stripslashes($_POST['shortDetail']);
           	$_POST['mediumDetail'] = stripslashes($_POST['mediumDetail']);
            $_POST['longDetail'] = stripslashes($_POST['longDetail']);
            $param = $_POST;
            $appService->insertApp($context, $result, $param);
            header("location:".WEB_BASE_URL."/index.php?controller=app&action=list");
        }
        else
        {
            $this->smarty->display('add-app.tpl');
        }    
    }

    public function updateAction()
    {
    	$_SESSION['timeout'] = time();
        $context = new BaseContext();
        $result = new ServiceResult();
        $appService = new AppService();
        if(isset($_GET['id']) && !empty($_GET['id']))
        {
            $id = $_GET['id'];
        }
        $app = $appService -> getAppById($context, $result, $id);
        $image = explode('/',$app['smallImage']);
        
        $img = $image[2];
        $type = $image[0];

        $this->smarty->assign('type', $type);
        $this->smarty->assign('img', $img);
        $this->smarty->assign('app', $app);
       
        if(isset($_POST) && !empty($_POST))
        {
        if ($_POST["type"] == 1)
            {
                $folder = 'ungdung';
            }
            else
            {
                $folder = 'tuvi';
            }

            $_POST['smallImage'] = $folder .'/'.'thumbnail'.'/'.$_POST['image'];
            $_POST['mediumImage'] = $folder .'/'.'small'.'/'.$_POST['image'];
            $_POST['largeImage'] = $folder .'/'.'large'.'/'.$_POST['image'];
			
           	$_POST['shortDetail'] = stripslashes($_POST['shortDetail']);
           	$_POST['mediumDetail'] = stripslashes($_POST['mediumDetail']);
			$_POST['longDetail'] = stripslashes($_POST['longDetail']);
			$_POST['status'] = $_POST["type"];
            $_POST['id'] = $_POST['id'];
            $param = $_POST;
            $appService->updateApp($context, $result, $param);

            header("location:".WEB_BASE_URL."/index.php?controller=app&action=list");
        }
     	$this->smarty->display('update-app.tpl');
    }
    
	public function deleteAction()
    {
        $_SESSION['timeout'] = time();
    	if(isset($_GET['id']) && !empty($_GET['id']))
		{
		  	$id = $_GET['id'];
		}
        $context = new BaseContext();
        $result = new ServiceResult();
       	$appService = new AppService();
        
        $appService->deleteApp($context, $result, $id);

        header("location:".WEB_BASE_URL."/index.php?controller=app&action=list");
    }

}

?>